package ensib.sti.ssu.classes;

import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class Parler implements OnInitListener {

    private TextToSpeech tts;
	private Context context;
	private String message;
public Context getContext() {
		return context;
	}
	public void setContext(Context context) {
		this.context = context;
	}
public Parler(Context context) {
		this(context,"");
	}
public TextToSpeech getTts() {
	return tts;
}
public Parler setTts(TextToSpeech tts) {
	this.tts = tts;
	return this;
}
public Parler(Context context,String message) {
		super();
		this.message = message;
		this.context=context;
		tts = new TextToSpeech(context, this);
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		if (status == TextToSpeech.SUCCESS) {
			 
            int result = tts.setLanguage( Locale.getDefault() );
 
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                
                parle();
            }
 
        } else {
            Log.e("TTS", "Initilization Failed!");
        }
	}
public String getMessage() {
		return message;
	}
	public Parler setMessage(String message) {
		this.message = message;
		return this;
	}
public void parle(){
	this.parle(getMessage()) ;
	  
     
}
public void parle(String message){
	if(this.iSpalyBackOn())
	 tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
    
}
public boolean iSpalyBackOn() {
	// TODO Auto-generated method stub
	SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getContext());
boolean	ret=prefs.getBoolean("activer_lecture_vocal_key", false);
Log.i("activer_lecture_vocal_key", ret+"");


        	return (ret)==true;
}
public boolean iSpalyBackOff() {
	// TODO Auto-generated method stub
 return !this.iSpalyBackOn();
}
public void arreter(){
	 if (tts != null) {
         tts.stop();
         tts.shutdown();
     }
}
}
